package base.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
	private static DateUtil dateUtilInstance;
	private SimpleDateFormat sdf;
	private Calendar cal=Calendar.getInstance();
	public final String formatLong="yyyy-MM-dd HH:mm:ss";
	public final String formatShort="yyyy-MM-dd";
	private DateUtil(){
	}
	
	public static DateUtil getInstance(){
		if(dateUtilInstance == null){
			dateUtilInstance = new DateUtil();
		}
		return dateUtilInstance;
	}
	
	public String getString(){
		return getString(formatLong);
	}
	
	public String getString(String format){
		sdf = new SimpleDateFormat(format);
        return sdf.format(getCurrentDate());
	}
	
	public String getString(Date date){
		sdf = new SimpleDateFormat(formatLong);
        return sdf.format(date);
	}

	public String getString(Date date,String format){
		sdf=new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public String GetShortDateString(){
		sdf = new SimpleDateFormat(formatShort);
		Date date = getCurrentDate();
	    return sdf.format(date);
	}

	public boolean isValidDate(String date){
		try{
			date=date.replace("/","-");
			sdf = new SimpleDateFormat(formatShort);
			sdf.setLenient(false);
			sdf.parse(date);
			return true;
		}catch(Exception e){
			return false;
		}
	}

	/**
	 * 取得当前时间
	 * @return
     */
	public Date getCurrentDate(){
		return new Date();
	}

	/**
	 * 取得当前时间
	 * @return
     */
	public Long getCurrentTime(){
		return getCurrentDate().getTime();
	}

	public Date getDateLong(String strDate) {
		sdf = new SimpleDateFormat(formatLong);
		try {
			return sdf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public Date getDateShort(String strDate) {
		sdf = new SimpleDateFormat(formatShort);
		try {
			return sdf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 取得月的第一天
	 * @param date
	 * @return
     */
	public Date getFirstDayOfMonth(Date date){
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH,1);
		cal.set(Calendar.HOUR,0);
		cal.set(Calendar.MINUTE,0);
		cal.set(Calendar.SECOND,0);
		return cal.getTime();
	}

	/**
	 * 取得月的第一天
	 * @param date
	 * @return
	 */
	public Date getEndDayOfMonth(Date date){
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		return cal.getTime();
	}

	public Date addMilliSecond(Date date, int milliSecond){
		cal.setTime(date);
		cal.add(Calendar.MILLISECOND, milliSecond);
		return cal.getTime();
	}

	/**
	 * 在日期上添加多几秒并返回日期.
	 *
	 * @param date
	 * @param second
	 * @return
	 */
	public Date addSecond(Date date, int second) {
		cal.setTime(date);
		cal.add(Calendar.SECOND, second);
		return cal.getTime();
	}

	/**
	 * 在日期上添加多几分钟并返回日期.
	 *
	 * @param date
	 * @param minute
	 * @return
	 */
	public Date addMinute(Date date, int minute) {
		return addSecond(date,minute*60);
	}

	/**
	 * 在日期上添加多几分钟并返回日期.
	 *
	 * @param date
	 * @return
	 */
	public Date addHour(Date date, int hour) {
		return addMinute(date,hour*60);
	}

	/**
	 * 在日期上添加多几天并返回日期.
	 *
	 * @param date
	 * @param days
	 * @return
	 */
	public Date addDay(Date date, int days) {
		return addHour(date,days*24);
	}

	/**
	 * 在日期上添加多几个月，并返回日期.
	 *
	 * @param date
	 * @param amount
	 * @return
	 */
	public Date addMonth(Date date, int amount) {
		cal.setTime(date);
		cal.add(Calendar.MONTH, amount);
		return cal.getTime();
	}

	/**
	 * 年龄小或者大多少岁的日期
	 *
	 * @param date
	 * @param year
	 * @return
	 */
	public Date addYear(Date date, int year) {
		cal.setTime(date);
		cal.add(Calendar.YEAR, year);
		return cal.getTime();
	}

	/**
	 * 计算两个日期的时间差
	 * @param startDate
	 * @param endDate
     * @return  返回两个小数点的数据  单位:小时
     */
	public double getTimeLong(Date startDate,Date endDate){
		double l=endDate.getTime()-startDate.getTime();
		double d=  l/(60*60*1000);
		d=(double)Math.round(d*100)/100;
		return d;
	}
}